package spotGUI.trayIcon;

import java.awt.Menu;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;

import managers.DBManager;
import managers.messengerManager.MessengerManager;
import managers.messengerManager.services.ChangeStateService;
import managers.messengerManager.services.Service;
import managers.socialNetworkManagers.ElementsManager;
import managers.socialNetworkManagers.SocialNetworkFeedbackManager;
import managers.socialNetworkManagers.SocialNetworkManager;
import spotGUI.optionsGUI.OptionsFrame;
import utils.StackTracePrinter;
import utils.UserState;
import elements.Element;


public class TrayPopup extends PopupMenu {
	
	private MenuItem mntmEsci;
	private Menu mnModificaStato;
	private MenuItem mntmModificaMessaggioPersonale;
	private MenuItem mntmRipristina;
	private MenuItem mnOpzioni;
	private ActionListener changeStateListener;

	private MessengerManager manager = MessengerManager.getSharedInstance();
	
	public TrayPopup() {
		
		mntmRipristina = new MenuItem("Mostra");
		mntmRipristina.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				manager.getFeedbackManager().feedbackNotification();
				
			}
		});
		add(mntmRipristina);
	
		addSeparator();
		
		mntmModificaMessaggioPersonale = new MenuItem(ResourceBundle.getBundle("spotGUI.trayIcon.messages").getString("DefaultTrayIconManager.popup1"));
		mntmModificaMessaggioPersonale.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String message = JOptionPane.showInputDialog(null, ResourceBundle.getBundle("spotGUI.trayIcon.messages").getString("DefaultTrayIconManager.popup2"),
						ResourceBundle.getBundle("spotGUI.trayIcon.messages").getString("DefaultTrayIconManager.popup3"), JOptionPane.PLAIN_MESSAGE);
				if (message == null)
					return;
				try {
					MessengerManager.getSharedInstance().getDataManager().updatePersonalMessage(message);
					ElementsManager.addElement(DBManager.getConnection(), new Element(Element.PERSONAL_MESSAGE,
							SocialNetworkManager.getSharedInstance().getUserId(), ResourceBundle.getBundle("spotGUI.trayIcon.messages").getString("DefaultTrayIconManager.popup4"), message, null));
					SocialNetworkManager.getSharedInstance().getUser(SocialNetworkManager.getSharedInstance().getUserId()).setPersonalMessage(message);
				} catch (SQLException e1) {
					SocialNetworkManager.getSharedInstance().getFeedbackManager().updateStatus(
							ResourceBundle.getBundle("spotGUI.trayIcon.messages").getString("DefaultTrayIconManager.popup5"), SocialNetworkFeedbackManager.WARNING);
					StackTracePrinter.print(e1);
				}
				SocialNetworkManager.getSharedInstance().getFeedbackManager().update();
			}
		});
		add(mntmModificaMessaggioPersonale);
		
		mnModificaStato = new Menu(ResourceBundle.getBundle("spotGUI.trayIcon.messages").getString("DefaultTrayIconManager.popup6"));
		add(mnModificaStato);
		
		
		changeStateListener = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				UserStateMenuItem menu = (UserStateMenuItem) e.getSource();
				Service changeStateService = new ChangeStateService(menu.getState());
				manager.addService(changeStateService);
				changeStateService.start();
			}
		};
		
		
		UserState[] values = UserState.values();
		for (int i = 0; i < values.length; i++) {
			UserStateMenuItem item = new UserStateMenuItem(values[i].getText(), values[i]);
			item.addActionListener(changeStateListener);
			mnModificaStato.add(item);
			}
		
		addSeparator();
		
		mnOpzioni = new MenuItem(ResourceBundle.getBundle("spotGUI.trayIcon.messages").getString("DefaultTrayIconManager.popup7"));
		mnOpzioni.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				new OptionsFrame();
			}
		});
		add(mnOpzioni);

		addSeparator();
		
		mntmEsci = new MenuItem(ResourceBundle.getBundle("spotGUI.trayIcon.messages").getString("DefaultTrayIconManager.popup8"));
		mntmEsci.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				DBManager.closeConnection();
				try {
					MessengerManager.getSharedInstance().closeConnection();
				} catch (Exception e) {
					//do nothing
					StackTracePrinter.print(e);
				}
				System.exit(0);
			}
		});
		add(mntmEsci);

	}
	
}
